<?php
if (!empty($_POST)) {
    if (isset($_POST['change_pwd'])) {
        $errors = array();
        $password = $_POST["password"];
        $password_new = $_POST["passwordc"];
        $password_confirm = $_POST["passwordcheck"];

        //Perform some validation
        //Feel free to edit / change as required

        if (trim($password) == "") {
            $errors[] = "Current password is required.";
        }
        if (trim($password_new) == "") {
            $errors[] = "New password is required.";
        } else if (minMaxRange(8, 50, $password_new)) {
            $errors[] = "New password must be no fewer than 8 characters or greater than 50.";
        } else if ($password_new != $password_confirm) {
            $errors[] = "Password's entered do not match!";
        }

        //End data validation
        if (count($errors) == 0) {
            //Confirm the hash's match before updating a users password
            $entered_pass = generateHash($password, $loggedInUser->hash_pw);

            //Also prevent updating if someone attempts to update with the same password
            $entered_pass_new = generateHash($password_new, $loggedInUser->hash_pw);

            if ($entered_pass != $loggedInUser->hash_pw) {
                //No match
                $errors[] = "Current password doesn't match the one we have one record.";
            } else if ($entered_pass_new == $loggedInUser->hash_pw) {
                //Don't update, this fool is trying to update with the same password ¬¬
                $errors[] = "Nothing to update.";
            } else {
                //This function will create the new hash and update the hash_pw property.
                $loggedInUser->updatePassword($password_new);
            }
        }
    }
}
?>
<?php require_once 'read-xml.php'; ?>
<?php
define('LANG', 'en');
if (isset($_GET['lang'])) {
    $lang_code = $_GET['lang'];
    if (!in_array($lang_code, array('km', 'en', 'fr'))) {
        $lang_code = LANG;
    }
} else {
    $lang_code = 'km';
}
?>
<div class="navbar">
    <div class="navbar-inner">
        <div class="container">
            <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </a>
            <div class="nav-collapse">
                <ul class="nav">
                    <li class="active">
                        <a href="#">
                            <i class="icon-home"></i>
                            <?php echo $languages[$lang_code]['home'] ?>
                        </a></li>
                    <li><a href="#"><?php echo $languages[$lang_code]['about'] ?></a></li>
                    <li><a href="#"><?php echo $languages[$lang_code]['service'] ?></a></li>
                    <li><a href="#"><?php echo $languages[$lang_code]['contact'] ?></a></li>
                </ul>
                <ul class="nav pull-right">
                    <li class="divider-vertical"></li>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                            <?php echo $languages[$lang_code]['label'] ?>
                            <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            <?php
                            foreach ($languages as $key => $value) {
                                echo '<li><a href="?lang=' . $key . '">'
                                . $value['label']
                                . ' <span class="pull-right">(' . $key . ')</span>'
                                . '</a></li>';
                            }
                            ?>
                        </ul>
                    </li>
                </ul>
                <form class="navbar-search pull-right" action="">
                    <input type="text" class="search-query span2" placeholder="Search" id="acpro_inp0">
                </form>
                <?php if (isUserLoggedIn()) { ?>
                    <ul class="nav pull-right">
                        <li class="divider-vertical"></li>
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                                <?php echo $loggedInUser->display_username; ?>
                                <b class="caret"></b></a>
                            <ul class="dropdown-menu">
                                <li><a href="javascript:showChangePwdDialog();">Change password</a></li>
                                <li class="divider"></li>
                                <li><a href="logout.php">Logout</a></li>
                            </ul>
                        </li>
                        <li class="divider-vertical"></li>
                    </ul>

                    <div class="modal hide fade in" id="change_pwd_dialog">
                        <form name="newUser" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
                            <input type="hidden" name="change_pwd" value="1" />
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal">×</button>
                                <h3>Change your password</h3>
                            </div>
                            <div class="modal-body">
                                <input type="password" name="password" placeholder="Old password ..." /><br />
                                <input type="password" name="passwordc" placeholder="New password ..." /><br />
                                <input type="password" name="passwordcheck" placeholder="Re-type new password ..." /><br />
                            </div>
                            <div class="modal-footer">
                                <a href="#" class="btn" data-dismiss="modal">Close</a>
                                <input type="submit"  class="btn btn-primary" value="Register" />
                            </div>
                        </form>
                    </div>

                <?php } ?>
            </div><!-- /.nav-collapse -->
        </div>
    </div><!-- /navbar-inner -->
</div>

<script type="text/javascript">
    function showChangePwdDialog(){
        $('#change_pwd_dialog').modal('show'); 
    }
</script>
